package com.fastCms.business.frame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.frame.daos.AlbumContentMapper;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.entity.AlbumContent;
import com.fastCms.business.frame.pojo.po.AlbumContentQueryPO;
import com.fastCms.business.frame.pojo.po.AlbumContentSavePO;
import com.fastCms.business.frame.pojo.vo.AlbumContentVO;
import com.fastCms.business.frame.service.AlbumContentService;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户相册 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-06-05
 */
@Transactional

@Service
public class AlbumContentServiceImpl extends ServiceImpl<AlbumContentMapper, AlbumContent> implements AlbumContentService {
    @Resource
    private AlbumContentMapper albumContentMapper;

    @Override
    public PageInfo<AlbumContentVO> contentPage(PageParam<AlbumContentQueryPO> pageParam) {
        AlbumContentQueryPO recordPO = pageParam.getQueryFields();
        LambdaQueryWrapper<AlbumContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlbumContent::getAlbumId, recordPO.getAlbumId());
        PageInfo<AlbumContentVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
            .doSelectPageInfo(() -> {
                    albumContentMapper.selectList(queryWrapper);
                }
            );

        if (CollUtil.isNotEmpty(result.getList())) {
            List<AlbumContentVO> albumContentVOS = BeanConvertUtil.convertList(result.getList(), AlbumContentVO.class);
            return new PageInfo<>(albumContentVOS);
        }
        return new PageInfo<>(Lists.newArrayList());

    }

    @Override
    public Boolean contentDelete(String id) {
        int i = albumContentMapper.deleteById(id);
        return true;
    }

    @Override
    public Boolean contentAdd(AlbumContentSavePO param) {
        String id = param.getId();
        AlbumContent albumContent;
        if (StringUtils.isBlank(id)) {
            albumContent = new AlbumContent();
            BeanUtil.copyProperties(param, albumContent);
        } else {
            albumContent = getById(id);
            BeanUtil.copyProperties(param, albumContent);
        }
        this.saveOrUpdate(albumContent);
        return true;
    }
}
